package com.sky.aspect;


import com.sky.context.BaseContext;
import com.sky.enumeration.OperationType;
import com.sky.myannotation.MyAutoFill;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MemberSignature;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.time.LocalDateTime;


@Component
@Aspect
public class MyAutoFillAspect {
    private final Logger logger = LogManager.getLogger(MyAutoFillAspect.class);
    @Pointcut("@annotation(com.sky.myannotation.MyAutoFill)")
    public void autofillPointCut(){

    }
    @Before("autofillPointCut()")
    public void autoFill(JoinPoint joinPoint){
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        MyAutoFill annotation = signature.getMethod().getAnnotation(MyAutoFill.class);
        OperationType value = annotation.value();
        Object[] args = joinPoint.getArgs();
        if(args == null || args.length == 0){
            return;
        }
        Object pojo = args[0]; // 拿到实体对象
        if(value.equals(OperationType.INSERT)){
            logger.info("切面正在执行"+value+"方法");
            try {
                Method setUpdateTime = pojo.getClass().getDeclaredMethod("setUpdateTime", LocalDateTime.class);
                Method setUpdateUser = pojo.getClass().getDeclaredMethod("setUpdateUser", Long.class);
                Method setCreateUser = pojo.getClass().getDeclaredMethod("setCreateUser", Long.class);
                Method setCreateTime = pojo.getClass().getDeclaredMethod("setCreateTime", LocalDateTime.class);
                setUpdateTime.invoke(pojo, LocalDateTime.now());
                setUpdateUser.invoke(pojo, BaseContext.getCurrentId());
                setCreateUser.invoke(pojo, BaseContext.getCurrentId());
                setCreateTime.invoke(pojo, LocalDateTime.now());

            } catch (NoSuchMethodException e) {
                throw new RuntimeException(e);
            } catch (InvocationTargetException e) {
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }else{
            try {
                Method setUpdateTime = pojo.getClass().getDeclaredMethod("setUpdateTime", LocalDateTime.class);
                Method setUpdateUser = pojo.getClass().getDeclaredMethod("setUpdateUser", Long.class);
                setUpdateTime.invoke(pojo, LocalDateTime.now());
                setUpdateUser.invoke(pojo, BaseContext.getCurrentId());
            } catch (NoSuchMethodException e) {
                throw new RuntimeException(e);
            } catch (InvocationTargetException e) {
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
            logger.info("切面正在执行"+value+"方法");
        }
    }

}
